use thiserror_no_std::Error;

use crate::c::{osStatus_t_osError, osStatus_t_osErrorISR, osStatus_t_osErrorNoMemory, osStatus_t_osErrorParameter, osStatus_t_osErrorResource, osStatus_t_osErrorTimeout, osStatus_t_osOK, osStatus_t_osStatusReserved};

/**
pub const osFlagsError: u32 = 2147483648;
pub const osFlagsErrorUnknown: u32 = 4294967295;
pub const osFlagsErrorTimeout: u32 = 4294967294;
pub const osFlagsErrorResource: u32 = 4294967293;
pub const osFlagsErrorParameter: u32 = 4294967292;
pub const osFlagsErrorISR: u32 = 4294967290;
 */

#[derive(Debug, Error)]
pub enum Error {
    Unknown,
    Error,
    Timeout,
    Resource,
    Parameter,
    ISR,
    NoMemory,
    StatusReserved,
}

#[inline(always)]
pub fn parse(r: u32) -> Result<u32, Error> {
    match r {
        4294967295 => Err(Error::Unknown),
        4294967294 => Err(Error::Timeout),
        4294967293 => Err(Error::Resource),
        4294967292 => Err(Error::Parameter),
        _ => Ok(r)
    }
}

#[allow(non_upper_case_globals)]
pub fn parse_empty(r: i32) -> Result<(), Error> {
    match r {
        osStatus_t_osOK => Ok(()),
        osStatus_t_osErrorTimeout => Err(Error::Timeout),
        osStatus_t_osErrorResource => Err(Error::Resource),
        osStatus_t_osErrorParameter => Err(Error::Parameter),
        osStatus_t_osErrorNoMemory => Err(Error::NoMemory),
        osStatus_t_osErrorISR => Err(Error::ISR),
        osStatus_t_osError => Err(Error::Error),
        osStatus_t_osStatusReserved => Err(Error::StatusReserved),
        _ => Err(Error::Unknown),
    }
}

#[allow(non_upper_case_globals)]
pub fn parse_val<T>(r: i32, v: T) -> Result<T, Error> {
    match r {
        osStatus_t_osOK => Ok(v),
        osStatus_t_osErrorTimeout => Err(Error::Timeout),
        osStatus_t_osErrorResource => Err(Error::Resource),
        osStatus_t_osErrorParameter => Err(Error::Parameter),
        osStatus_t_osErrorNoMemory => Err(Error::NoMemory),
        osStatus_t_osErrorISR => Err(Error::ISR),
        osStatus_t_osError => Err(Error::Error),
        osStatus_t_osStatusReserved => Err(Error::StatusReserved),
        _ => Err(Error::Unknown),
    }
}
